package com.sdap2.common.interceptor;

import com.sdap2.common.context.RequestContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.UUID;

@Slf4j
@Component
public class RequestInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String requestId = UUID.randomUUID().toString();
        RequestContext.setRequestId(requestId);
        log.info("Request start - [{}] {} {}", requestId, request.getMethod(), request.getRequestURI());
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        log.info("Request end - [{}] {} {} - {}", 
            RequestContext.getRequestId(), 
            request.getMethod(), 
            request.getRequestURI(), 
            response.getStatus());
        RequestContext.clear();
    }
} 